package com.yk.faceapplication.activity;

import android.location.Location;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.allinpay.xed.R;
import com.android.volley.VolleyError;
import com.yk.faceapplication.callback.RequestCallback;
import com.yk.faceapplication.http.RequestUrl;
import com.yk.faceapplication.util.ActivityUtil;
import com.yk.faceapplication.util.RequestUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yk_2018/1/12_16:29
 */

public class SetPwdActivity extends BaseActivity{
    private Button commitBtn;
    private EditText pwdET,pwdAgainET;
    private Location location;
    private ImageView eyeIV,eyeAgainIV;
    private boolean eyeIsOpen = false;
    private boolean eyeAgainIsOpen = false;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setTitle("找回密码");
        eyeIV = (ImageView) findViewById(R.id.pwd_eye_img);
        eyeIV.setOnClickListener(this);
        eyeAgainIV = (ImageView) findViewById(R.id.pwd_again_eye_img);
        eyeAgainIV.setOnClickListener(this);
        commitBtn = (Button) findViewById(R.id.commit_btn);
        pwdET = (EditText) findViewById(R.id.pwd_et);
        pwdAgainET = (EditText) findViewById(R.id.pwd_again_et);
        commitBtn.setOnClickListener(this);
        location = mApplication.getLocation();
        pwdET.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 输入前的监听

            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入后的监听
                checkInputInfo();
            }
        });
        pwdAgainET.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 输入前的监听

            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入后的监听
                checkInputInfo();
            }
        });
        checkInputInfo();
    }
    private void checkInputInfo(){
        if(ActivityUtil.editIsNull(pwdAgainET) || ActivityUtil.editIsNull(pwdET)){
            commitBtn.setBackgroundColor(getResources().getColor(R.color.bg_gray));
            commitBtn.setEnabled(false);
        }else{
            if(pwdAgainET.getText().toString().trim().equals(pwdET.getText().toString().trim()) && pwdET.getText().toString().trim().length()>5 && ActivityUtil.isPassworkFormat(pwdET.getText().toString().trim())) {
                commitBtn.setBackgroundColor(getResources().getColor(R.color.textRegisterFile));
                commitBtn.setEnabled(true);
            }else{
                commitBtn.setBackgroundColor(getResources().getColor(R.color.bg_gray));
                commitBtn.setEnabled(false);
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch(v.getId()){
            case R.id.commit_btn:
                if(ActivityUtil.editIsNull(pwdET) || ActivityUtil.editIsNull(pwdAgainET) ){
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!pwdET.getText().toString().trim().equals(pwdAgainET.getText().toString().trim())){
                    Toast.makeText(this, "两次输入密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                setPwd();
                break;
            case R.id.pwd_eye_img:
                if(eyeIsOpen){
                    eyeIV.setImageResource(R.drawable.pwd_close_icon);
                    pwdET.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    pwdET.setSelection(pwdET.getText().toString().length());
                }else{
                    eyeIV.setImageResource(R.drawable.pwd_close_open);
                    pwdET.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    pwdET.setSelection(pwdET.getText().toString().length());
                }
                eyeIsOpen = !eyeIsOpen;
                break;
            case R.id.pwd_again_eye_img:
                if(eyeAgainIsOpen){
                    eyeAgainIV.setImageResource(R.drawable.pwd_close_icon);
                    pwdAgainET.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    pwdAgainET.setSelection(pwdAgainET.getText().toString().length());
                }else{
                    eyeAgainIV.setImageResource(R.drawable.pwd_close_open);
                    pwdAgainET.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    pwdAgainET.setSelection(pwdAgainET.getText().toString().length());
                }
                eyeAgainIsOpen = !eyeAgainIsOpen;
                break;
        }
    }


    //    设置密码
    private void setPwd(){
        String latitude = location==null?"0":location.getLatitude()+"";
        String longitude = location==null?"0":location.getLongitude()+"";
        Map<String, String> form = new HashMap<String, String>();
        form.put("mobile", getIntent().getStringExtra("mobile"));
        form.put("newPassword", pwdET.getText().toString());
        form.put("latitude", latitude);
        form.put("longitude", longitude);
        form.put("userAgentCode","2");
        RequestCallback callback = new RequestCallback() {
            @Override
            public void success(JSONObject jsonObject) {
                Toast.makeText(SetPwdActivity.this, "密码设置成功",Toast.LENGTH_SHORT).show();
                setResult(RESULT_OK);
                finishAct();
            }

            @Override
            public void fail(VolleyError volleyError) {

            }
        };
        RequestUtil.request(this,RequestUrl.SETPWD,callback,form);
    }


}
